;
(function (window, angular) {
	'use strict';
	angular.module("Controller.System.AppSet", [])
		.controller("AppSetCtrl", ["$scope", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", 'Upload', "UserContextService", "ybConfig", "$timeout", function ($scope, ybWidget, ybHttp, MESG_TYPE, $filter, Upload, UserContextService, ybConfig, $timeout) {
			
			$scope.toolbarConfig = {
				showAdvanceSearch: false
			};
			$scope.switchToolbar = function (toolbar) {
				if (!!$scope.toolbarConfig["show" + toolbar]) {
					$scope.toolbarConfig["show" + toolbar] = false;
				} else {
					$timeout(function () {
						$scope.toolbarConfig["show" + toolbar] = true;
					});
				}
			};
			$scope.reset = function () {
				$scope.condition = {};
			};

			$scope.activeFlag = [true, false, false];
			$scope.markInfo = {
				curRow: -1
			};
			$scope.pageInfo = {
				totalPages: 1,
				totalRows: 0,
				pageSize: 10,
				curPage: 1
			};
			$scope.submitted = false;
			$scope.condition = {};
			$scope.appTypeMap = {
				1: 'PDA',
			};

			$scope.dataList = [];
			$scope.detailInfo = {
				type: 1,
				name: "广汽仓储" + $scope.appTypeMap[1],
				isRigidUpdate: false
			};
			$scope.tbConf = {
				scrollWidth: 1300,
				tableCol: [
					{
						type: "operate",
						name: "操作",
						dataField: "",
						width: 80,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "名称",
						dataField: "name",
						width: 150,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "类型",
						dataField: "typeShow",
						width: 100,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "版本号",
						dataField: "version",
						width: 100,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "是否强制升级",
						dataField: "status",
						width: 100,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "发布时间",
						dataField: "createDateShow",
						width: 140,
						isShow: true,
						isFixed: false
					},
					{
						type: "",
						name: "版本说明",
						dataField: "note",
						width: 600,
						isShow: true,
						isFixed: false
					}
				]
			};

			var btnPermissionCode = {
				appSettingAdd: window.ActionCode.appSettingAdd,
				appSettingEdit: window.ActionCode.appSettingEdit
			};

			$scope.btnPermission = {
				appSettingAdd: true,
				appSettingEdit: true
			};

			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			$scope.changeTab = function (index) {
				$scope.activeFlag = [false, false, false];
				$scope.activeFlag[index] = true;
				$scope.submitted = false;
				switch (index) {
					case 0:
						initData();
						break;
					case 1:
						break;
					case 2:
						break;
					default:
						break;
				}
			};

			function initData() {
				$scope.fileFormatErr = false;
				$scope.fileUploadErr = false;

				$scope.detailInfo = {
					type: 1,
					name: "广汽仓储" + $scope.appTypeMap[1],
					isRigidUpdate: false
				};
			}

			function getDataList(curPage) {
				$scope.markInfo = {
					curRow: -1
				};
				var service = "device";
				var reqUrl = "/appSetting/list";
				var param = {};
				param.pageSize = $scope.pageInfo.pageSize;
				if (!!curPage) {
					$scope.pageInfo.curPage = curPage;
				}
				param.pageNum = $scope.pageInfo.curPage;
				param.filters = angular.copy($scope.condition);
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value.list, function (item) {
						item.isChecked = false;
						item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
						item.typeShow = $scope.appTypeMap[item.type];				
					});
					$scope.pageInfo.totalRows = resp.value.total;
					$scope.pageInfo.totalPages = resp.value.pages;
					$scope.dataList = resp.value.list;
				});
			}

			$scope.selectRow = function (index) {
				$scope.markInfo.curRow = index
			};

			$scope.search = function (curpage) {
				getDataList(curpage);
			};

			$scope.addData = function () {
				$scope.changeTab(1);
			};

			$scope.delData = function (delType, billId) {
				var service = "device";
				var reqUrl = "/abnormalOut/remove";
				var billIds = [];
				if (delType === "single") {
					billIds.push(billId);
				}
				if (delType === "batch") {
					for (var i = 0, len = $scope.dataList.length; i < len; i++) {
						if (!!$scope.dataList[i].isChecked) {
							billIds.push($scope.dataList[i].id);
						}
					}
				}
				if (billIds.length === 0) {
					ybWidget.ShowMessage('请选择需要删除的行');
					return;
				}
				ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
					ybHttp.Post(service, reqUrl, billIds).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						getDataList();
					});
				});
			};

			function getDetail(billId) {
				var service = "device";
				var reqUrl = "/appSetting/item";
				var param = $scope.condition;
				param.id = billId;
				return ybHttp.Get(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return false;
					}
					return resp.value;
				});
			}

			$scope.editData = function (item) {
				getDetail(item.id).then(function (data) {
					if (!data) {
						return;
					}
					$scope.changeTab(2);
					$scope.orderObj = data;
				});
			};

			$scope.$watch('detailInfo.type', function(newVal) {
				if(newVal) {
					$scope.detailInfo.name = "广汽仓储" + $scope.appTypeMap[newVal];
				}
			});

			$scope.checkFileType = function (file) {
				$scope.fileFormatErr = false;
				$scope.fileUploadErr = false;

				if (!file || !/\.(apk|APK)$/.test(file.name)) {
					$scope.fileFormatErr = true;
					return;
				}

				if ($scope.detailInfo.type == 1) {
					$scope.detailInfo.version = file.name.split('.apk')[0].split('_')[1];
				}

				$scope.$emit('showLoading', '上传中...');
				Upload.upload({
					url: ybConfig.getConfig().baseUrl.Site + ybConfig.getConfig().serviceName.device + '/minio/uploadFile',
					data: { imgFile: file }
				}).then(function (resp) {
					$scope.$emit('hideLoading');
					var resData = resp.data;
					if (resData.code !== 0) {
						$scope.fileUploadErr = true;
						ybWidget.ShowMessage(resData, MESG_TYPE.Error);
					} else {
						ybWidget.ShowToaster('success', '上传成功');
						file.apkUrl = resData.value.url;
					}
				}, function () {
					$scope.$emit('hideLoading');
					$scope.fileUploadErr = true;
				})
			};

			$scope.saveData = function (isValid, file) {
				if (!isValid || $scope.fileFormatErr || $scope.fileUploadErr) {
					$scope.submitted = true;
					return;
				}

				var service = "device";
				var reqUrl = "/appSetting/insert";

				$scope.detailInfo.fileName = file.name;
				$scope.detailInfo.updateUrl = file.apkUrl;
				ybHttp.Post(service, reqUrl, $scope.detailInfo).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					ybWidget.ShowToaster('success', '操作成功');
					getDataList(1);
					$scope.changeTab(0);
				});
			};

			$scope.updateData = function (isValid) {
				if (!isValid) {
					$scope.submitted = true;
					return;
				}
				var service = "device";
				var reqUrl = "/appSetting/update";
				var param = angular.copy($scope.orderObj);
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					ybWidget.ShowToaster('success', '操作成功');
					getDataList();
					$scope.changeTab(0);
				});

			};

			$scope.cancel = function () {
				$scope.changeTab(0);
			};

			(function () {
				getDataList();
				setBtnPermission();
			})();
		}])
	;
})(window, window.angular);